#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

int countBeautifulPairs(vector<int>& nums) {
    int res = 0, cnt[10]{};
    for (int x : nums) {
        for (int i = 1; i <= 9; i++)
            if (gcd(x % 10, i) == 1)
                res += cnt[i];
        while (x >= 10)
            x /= 10;
        cnt[x]++;
    }
    return res;
}